/**
 * @param {string[]} words
 * @param {string} order
 * @return {boolean}
 */
var isAlienSorted = function (words, order) {
	const map = new Map()
	order.split('').forEach((_, i) => {
		map.set(_, i)
	})
	for (let i = 0; i < words.length - 1; i++) {
		const cur = words[i],
			next = words[i + 1]

		let m = 0
		const len = Math.max(cur.length, next.length)
		while (m < len) {
			if (cur[m] !== next[m]) {
				if (cur[m] == null) return true
				if (next[m] == null) return false
				if (map.get(cur[m]) > map.get(next[m])) {
					return false
				}
				break
			}
			m++
		}
	}
	return true
}
var isAlienSorted = function (words, order) {
	const index = new Array(26).fill(0)
	for (let i = 0; i < order.length; i++) {
		index[order[i].charCodeAt() - 'a'.charCodeAt()] = i
	}
	for (let i = 1; i < words.length; i++) {
		let valid = false
		for (let j = 0; j < words[i - 1].length && j < words[i].length; j++) {
			const prev = index[words[i - 1][j].charCodeAt() - 'a'.charCodeAt()]
			const cur = index[words[i][j].charCodeAt() - 'a'.charCodeAt()]
			if (prev < cur) {
				valid = true
				break
			} else if (prev > cur) return false
		}
		/* 处理空串 */
		if (!valid) {
			/* 比较两个字符串的长度 */
			if (words[i - 1].length > words[i].length) {
				return false
			}
		}
	}
	return true
}
const ret = isAlienSorted(
	(words = ['word', 'world', 'row']),
	'worldabcefghijkmnpqstuvxyz'
)
console.log('isAlienSorted :>>', ret)
